package com.yjf.ljr.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.adapter.ViewPagerAdapter;
import com.yjf.ljr.util.MyViewPager;
import com.yjf.ljr.util.PicLoad;

public class BigPicActivity extends IjActivity {

	private MyViewPager mVpBigPic;
	private List<View> mAdvPics = new ArrayList<View>();
	private List<String> mPicUrls = new ArrayList<String>();
	private ViewPagerAdapter mBigPicAdpter;
	private int mIndexStart = 0;
	private TextView mTvTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_big_pic);

		getIntentParams();
		initControls();
	}

	protected void getIntentParams() {
		mIndexStart = getIntent().getIntExtra("index", 1);
		mPicUrls = getIntent().getStringArrayListExtra("picUrls");
		mTvTitle = (TextView) findViewById(R.id.tvTitle_titlelayout);
		if (mPicUrls != null) {
			mTvTitle.setText((mIndexStart + 1) + "/" + mPicUrls.size());
		}
	}

	private void initControls() {
		findViewById(R.id.btnLeft_titlelayout).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						finish();
					}
				});
		mVpBigPic = (MyViewPager) findViewById(R.id.viewpagerCompanyIntroduce);

		for (int i = 0; i < mPicUrls.size(); i++) {
			View rv = View.inflate(this, R.layout.big_pic_loading, null);
			final ImageView ivCompanyPic = (ImageView) rv
					.findViewById(R.id.iv_big_pic_loading);
			final ProgressBar pb = (ProgressBar) rv
					.findViewById(R.id.probar_big_pic_loading);
			pb.setVisibility(View.GONE);
			mAdvPics.add(rv);

			// 加载网络图片
			String picUrl = mPicUrls.get(i);
			if (!TextUtils.isEmpty(picUrl)) {
				PicLoad.getImage(ivCompanyPic, "FamousCompany", picUrl, pb);
			}
		}

		mBigPicAdpter = new ViewPagerAdapter(mAdvPics);
		mVpBigPic.setAdapter(mBigPicAdpter);
		mVpBigPic.setOnPageChangeListener(new GuidePageChangeListener());
		mVpBigPic.setCurrentItem(mIndexStart);
	}

	private final class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageSelected(int arg0) {
			if (mPicUrls != null) {
				mTvTitle.setText((arg0 + 1) + "/" + mPicUrls.size());
			}
		}
	}
}
